package com.android.clinic.app.ui.main.view;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.android.clinic.app.R;
import com.android.clinic.app.entity.BannerInfo;
import com.android.clinic.app.glide.ImageLoaderUtils;
import com.android.clinic.app.ui.personal.WebViewActivity;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;
import com.youth.banner.view.BannerViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cheng on 2018/1/26.
 */
public class BannerView extends LinearLayout {

    private Banner banner;
    private List<String> mPath = new ArrayList<>();
    private int bannerStyle = 1;//图片标点样式 默认圆形
    private Activity activity;

    public BannerView(Context context) {
        super(context);
    }

    public BannerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public BannerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public int getBannerStyle() {
        return bannerStyle;
    }

    public void setBannerStyle(int bannerStyle) {
        this.bannerStyle = bannerStyle;
    }

    public List<String> getmPath() {
        return mPath;
    }

    public void init(Context context, int ow, int w, int h) {
        banner = (Banner) findViewById(R.id.banner);
        activity = (Activity) context;
        setLayout(ow, w, h);
    }

    public void setArrayList(final List<BannerInfo> bannerList) {
        mPath.clear();
        banner.releaseBanner();
        for (BannerInfo object : bannerList) {
            mPath.add(object.getTextUrl());
        }
        banner.setImages(mPath)
                .setIndicatorGravity(BannerConfig.CENTER)
                .setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
                .setImageLoader(new ImageLoader() {
                    @Override
                    public void displayImage(Context context, Object path, ImageView imageView) {
                        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                        ImageLoaderUtils.getInstance().loadNormalImage(path, imageView);
                    }
                })
                .setOnBannerListener(position -> {
                    if (position < bannerList.size()) {
                         // 点击事件处理
                        Intent intent = new Intent(activity, WebViewActivity.class);
                        intent.putExtra("path", bannerList.get(position).getLinkUrl());
                        activity.startActivity(intent);
                    }
                })
                .setDelayTime(4000)
                .start();
        if (getChildCount() == 0) {
            addView(banner);
        }
    }

    public void setLayout(int ow, int w, int h) {
        int itemHeight = (int) (ow * h * 1.0f / w);
        ViewGroup.LayoutParams params = null;
        if (banner.getLayoutParams() instanceof LinearLayout.LayoutParams) {
            params = new LinearLayout.LayoutParams(ow, itemHeight);
        } else if (banner.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
            params = new RelativeLayout.LayoutParams(ow, itemHeight);
        } else {
            params = new ViewGroup.LayoutParams(ow, itemHeight);
        }
        banner.setLayoutParams(params);
    }

    public void stopBanner() {
        banner.stopAutoPlay();
    }

    public void startBanner() {
        banner.startAutoPlay();
    }

    public void releaseBanner() {
        banner.releaseBanner();
    }

    public void removeBanner() {
        BannerViewPager viewPager = (BannerViewPager) banner.findViewById(R.id.bannerViewPager);
        int count = viewPager.getChildCount();
        for (int i = 0; i < count; i++) {
            View view = viewPager.getChildAt(i);
            if (view instanceof ImageView) {
                ImageView iv = (ImageView) view;
                iv.setImageBitmap(null);
            }
        }
        viewPager.setAdapter(null);
        viewPager.removeAllViews();
        removeAllViews();
    }

}
